package org.vectrics.recruiter.mail.template.struts;

import java.util.Map;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.common.util.StringUtils;
import org.vectrics.mail.struts.TemplateParameterLoader;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.struts.Request;

public class JobTemplateParameterLoader implements TemplateParameterLoader {
	private Logger log = Logger.getLogger(this.getClass());

	public JobTemplateParameterLoader() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void loadParameters(Request request, Map parameterMap) {
		String jobIdParam = request.getParameter("jobId");
		if (StringUtils.isNullOrBlank(jobIdParam))
			throw new SystemException("Template Parameter Loader require a 'jobId' parameter to be passed as part of the request");
		Long jobId = new Long(jobIdParam);
		Job job = RecruitServiceLocator.getJobProvider().findJob(jobId);
		if (job == null) {
			throw new SystemException("Could not find job needed for template: " + jobIdParam);
		}
		parameterMap.put("jobId", jobIdParam);
		log.info("Select job added to parameter list, jobId=" + jobId);
	}

	/**
	 * 
	 */
	public void loadParameterObject(String propertyName, String propertyValue, 
				Map<String, Object> parameterMap) {
		if (propertyName.equals("jobId")) {
			Long jobId = new Long(propertyValue);
			Job job = RecruitServiceLocator.getJobProvider().findJob(jobId);
			parameterMap.put("job", job);
		} else {
			throw new SystemException("Do not know how to handle parameter: " + propertyName);
		}
	}
	
}
